package org.urbansoft.icl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;
import org.urbansoft.icl.record.ImageViewAnalysisRecord;
import org.urbansoft.icl.record.ImageViewDataRecord;
import org.urbansoft.icl.record.ImageViewDetailRecord;
import org.urbansoft.iso.ISOUtil;

/**
 * The Class ImageView.
 */
public class ImageView {
	private ImageViewDetailRecord imageViewDetail;
	private ImageViewDataRecord imageViewData;
	private ImageViewAnalysisRecord imageViewAnalysis;

	private static Logger log = Logger.getLogger(ImageView.class);

	/**
	 * Instantiates a new image view.
	 */
	public ImageView() {
	}

	/**
	 * Instantiates a new image view.
	 * 
	 * @param imageViewDetail
	 *            the image view detail
	 * @param imageViewData
	 *            the image view data
	 * @param imageViewAnalysis
	 *            the image view analysis
	 */
	public ImageView(ImageViewDetailRecord imageViewDetail,
			ImageViewDataRecord imageViewData,
			ImageViewAnalysisRecord imageViewAnalysis) {
		super();
		this.imageViewDetail = imageViewDetail;
		this.imageViewData = imageViewData;
		this.imageViewAnalysis = imageViewAnalysis;
	}

	/**
	 * Save image.
	 * 
	 * @param f
	 *            the f
	 * 
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	public void saveImage(String path) throws IOException {
		try {
			String frontRear = "";
			String fileSeparator = System.getProperty("file.separator");

			/* prepend and F or R for front or rear view */
			if (imageViewDetail.viewSideIndicator.getFieldData().equals("0")) {
				frontRear = "F";
			} else {
				frontRear = "R";
			}

			/* build the filename using the sequence number as identifier */
			String fileName = path
					+ fileSeparator
					+ frontRear
					+ imageViewData.eceInstitutionItemSequenceNumber
							.getFieldData().trim() + ".tiff";

			FileOutputStream fOut = new FileOutputStream(new File(fileName));
			fOut.write(ISOUtil.asciiToEbcdic(imageViewData.imageData
					.getFieldDataBytes()));
			fOut.close();
		} catch (IOException e) {
			log.error("Error writing to file", e);
			throw e;
		}
	}

	/**
	 * Save image.
	 * 
	 * @param f
	 *            the f
	 * 
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	public String saveImageFull(String path) throws IOException {
		try {
			String frontRear = "F";
			String fileSeparator = System.getProperty("file.separator");

			/* prepend and F or R for front or rear view */
			if (imageViewDetail != null) {
				if (imageViewDetail.viewSideIndicator.getFieldData()
						.equals("0")) {
					frontRear = "F";
				} else {
					frontRear = "R";
				}
			}

			/* build the filename using the sequence number as identifier */
			String onlyName = frontRear
					+ imageViewData.eceInstitutionItemSequenceNumber
							.getFieldData().trim() + ".tiff";
			ResourceBundle bundle = ResourceBundle
					.getBundle("com.urbansoft.propiedades.Parametros");
			String file_dir = bundle.getString("imgX9Dir");
			boolean dir = new File(file_dir).mkdirs();
			String fileName = file_dir + fileSeparator + onlyName;

			FileOutputStream fOut = new FileOutputStream(new File(fileName));
			fOut.write(ISOUtil.asciiToEbcdic(imageViewData.imageData
					.getFieldDataBytes()));
			fOut.close();
			return onlyName;
		} catch (IOException e) {
			log.error("Error writing to file", e);
			throw e;
		}
	}

	/**
	 * Gets the image view detail.
	 * 
	 * @return the image view detail
	 */
	public ImageViewDetailRecord getImageViewDetail() {
		return imageViewDetail;
	}

	/**
	 * Sets the image view detail.
	 * 
	 * @param imageViewDetail
	 *            the new image view detail
	 */
	public void setImageViewDetail(ImageViewDetailRecord imageViewDetail) {
		this.imageViewDetail = imageViewDetail;
	}

	/**
	 * Gets the image view data.
	 * 
	 * @return the image view data
	 */
	public ImageViewDataRecord getImageViewData() {
		return imageViewData;
	}

	/**
	 * Sets the image view data.
	 * 
	 * @param imageViewData
	 *            the new image view data
	 */
	public void setImageViewData(ImageViewDataRecord imageViewData) {
		this.imageViewData = imageViewData;
	}

	/**
	 * Gets the image view analysis.
	 * 
	 * @return the image view analysis
	 */
	public ImageViewAnalysisRecord getImageViewAnalysis() {
		return imageViewAnalysis;
	}

	/**
	 * Sets the image view analysis.
	 * 
	 * @param imageViewAnalysis
	 *            the new image view analysis
	 */
	public void setImageViewAnalysis(ImageViewAnalysisRecord imageViewAnalysis) {
		this.imageViewAnalysis = imageViewAnalysis;
	}

}
